<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    // 判断用户是在 移动app 手机浏览器 桌面浏览器 打开
    export function isBrowserApp() {
      // 获取用户代理字符串
      var userAgent = navigator.userAgent;
      // 判断是否是移动设备
      var isMobile = /Mobile/.test(userAgent) || /Android/.test(userAgent);
      // 判断是否是在应用程序中打开
      var isApp = /AppName/.test(userAgent); // 用实际应用程序名称替换"AppName"
      if (isMobile) {
        if (isApp) {
          console.log("用户在移动应用中打开");
        } else {
          console.log("用户在移动浏览器中打开");
        }
      } else {
        console.log("用户在桌面浏览器中打开");
      }
    }

    /**
     * 判断手机是Android还是IOS
     *  1: android
     *  2: ios
     */
    export function isAndOrIos() {
      const u = navigator.userAgent
      const isAndroid = u.indexOf('Android') > -1 || u.indexOf('Linux') > -1
      const isIOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)
      if (isIOS) {
        return 2
      }
      if (isAndroid) {
        return 1
      }
      return -1
    }

    /**
     * 是否是微信或QQ内置浏览器
     * @returns Boolean
     */
    export function isWXOrQQBrowser() {
      const ua = navigator.userAgent.toLowerCase()
      //qq内置浏览器
      if (ua.indexOf(' qq') > -1 && ua.indexOf('mqqbrowser') < 0) return true
      //qq浏览器
      if (ua.indexOf('mqqbrowser') > -1 && ua.indexOf(' qq') < 0) return false
      //微信浏览器
      if (ua.indexOf('micromessenger') > -1) return true
      return !!ua.match(/QQ/i)
    }

    /**
     * 版本号比较
     * @returns 0: v1 === v2; 1:v1 > v2; -1: v1 < v2
     * @param version1
     * @param version2
     */
    export function compareVersion(version1, version2) {
      const v1 = version1.split('.')
      const v2 = version2.split('.')
      const len = Math.max(v1.length, v2.length)
      // 调整两个版本号位数相同
      while (v1.length < len) {
        v1.push('0')
      }
      while (v2.length < len) {
        v2.push('0')
      }
      // 循环判断每位数的大小
      for (let i = 0; i < len; i++) {
        const num1 = parseInt(v1[i])
        const num2 = parseInt(v2[i])
        if (num1 > num2) {
          return 1
        } else if (num1 < num2) {
          return -1
        }
      }
      return 0
    }

    /**
     *  获取系统
     */
    export function getOsVersion() {
      let u = navigator.userAgent,
        version = ''
      if (u.indexOf('Mac OS X') > -1) {
        // ios
        const regStr_saf = /OS [\d._]*/gi
        const info = u.match(regStr_saf)
        version = 'IOS' + (info + '').replace(/[^0-9|_.]/ig, '').replace(/_/ig, '.')
      } else if (u.indexOf('Android') > -1 ||
        u.indexOf('Linux') > -1) {
        // android
        version = 'Android' + u.substr(u.indexOf('Android') + 8, u.indexOf(';', u.indexOf('Android')) - u.indexOf(
          'Android') - 8)
      } else if (u.indexOf('BB10') > -1) {
        // 黑莓bb10系统
        version = 'blackberry' + u.substr(u.indexOf('BB10') + 5, u.indexOf(';', u.indexOf('BB10')) - u.indexOf('BB10') -
          5)
      } else if (u.indexOf('IEMobile') > -1) {
        // windows phone
        version = 'winphone' + u.substr(u.indexOf('IEMobile') + 9, u.indexOf(';', u.indexOf('IEMobile')) - u.indexOf(
          'IEMobile') - 9)
      } else {
        const userAgent = navigator.userAgent.toLowerCase()
        if (userAgent.indexOf('windows nt 5.0') > -1) {
          version = 'Windows 2000'
        } else if (userAgent.indexOf('windows nt 5.1') > -1 || userAgent.indexOf('windows nt 5.2') > -1) {
          version = 'Windows XP'
        } else if (userAgent.indexOf('windows nt 6.0') > -1) {
          version = 'Windows Vista'
        } else if (userAgent.indexOf('windows nt 6.1') > -1 || userAgent.indexOf('windows 7') > -1) {
          version = 'Windows 7'
        } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows 8') > -1) {
          version = 'Windows 8'
        } else if (userAgent.indexOf('windows nt 6.3') > -1) {
          version = 'Windows 8.1'
        } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows nt 10.0') > -1) {
          version = 'Windows 10'
        } else {
          version = 'Unknown'
        }
      }
      return version
    }


    // 判断用户是否使用移动设备（手机或平板电脑）  
    var isMobileDevice = /Mobi|Android/i.test(navigator.userAgent);

    // 判断用户是否使用电脑端  
    var isDesktopDevice = !isMobileDevice;

    // 判断用户使用的操作系统  
    var userAgent = navigator.userAgent;
    var isWindows = /Windows/i.test(userAgent);
    var isMacOS = /Macintosh|MacPPC|Mac OS|Mac_PowerPC|Macintosh/i.test(userAgent);
    var isLinux = /Linux/i.test(userAgent);

    // 判断用户使用的浏览器版本  
    var browserVersion = navigator.appVersion;

    // 输出结果  
    console.log("用户是否使用移动设备：" + isMobileDevice);
    console.log("用户是否使用电脑端：" + isDesktopDevice);
    console.log("用户使用的操作系统：");
    if (isWindows) {
      console.log("Windows");
    } else if (isMacOS) {
      console.log("MacOS");
    } else if (isLinux) {
      console.log("Linux");
    } else {
      console.log("其他操作系统");
    }
    console.log("用户使用的浏览器版本：" + browserVersion);




    // 判断用户当前使用的系统及软件类型以及版本号
    function getDeviceInfo() {
      var userAgent = navigator.userAgent;
      var platform = navigator.platform;
      var os = null;
      var browser = null;
      var version = null;

      if (/Mac/.test(platform)) {
        os = 'MacOS';
        browser = 'Safari';
        version = /Version\/(\d+)\.(\d+)\.(\d+)/.exec(userAgent)[3];
      } else if (/Win/.test(platform)) {
        os = 'Windows';
        browser = 'IE' in window ? 'IE' : 'Chrome';
        version = /MSIE (\d+\.\d+);/.test(userAgent) ? parseFloat(RegExp.$1) : /Chrome\/(\d+\.\d+)/.exec(userAgent)[1];
      } else if (/Linux/.test(platform)) {
        os = 'Linux';
        browser = 'Firefox';
        version = /Firefox\/(\d+\.\d+)/.exec(userAgent)[1];
      } else if (/Android/.test(platform)) {
        os = 'Android';
        browser = 'Chrome';
        version = /Chrome\/(\d+\.\d+)/.exec(userAgent)[1];
      } else if (/iPhone|iPad|iPod/.test(platform)) {
        os = 'iOS';
        browser = 'Safari';
        version = /Version\/(\d+)\.(\d+)\.(\d+)/.exec(userAgent)[3];
      }

      return {
        os: os,
        browser: browser,
        version: version
      };
    }

    var deviceInfo = getDeviceInfo();
    console.log('操作系统：', deviceInfo.os);
    console.log('浏览器：', deviceInfo.browser);
    console.log('版本：', deviceInfo.version);

  </script>
</body>

</html>